package com.mrent.service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import org.springframework.stereotype.Component;

import com.mrent.entity.AdminPayment;
import com.mrent.entity.ManagerPayment;

@Component
public class ManagerPaymentServiceImpl implements ManagerPaymentService{

	
	/**
	 * 保存店主支付方式.
	 * 
	 * @param member
	 */
	public void savePayment(ManagerPayment managerPayment){
		EntityManager em = EMF.getEM();
		EntityTransaction trans = em.getTransaction();
		try {
			trans.begin();
			em.persist(managerPayment);
		}catch(Exception e){
			System.err.println("异常问题");
			e.printStackTrace();
		}
		finally {
			trans.commit();
		}
	}
	
	/**
	 * 修改店主支付方式.
	 * 
	 * @param member
	 */
	public void updatePayment(ManagerPayment managerPayment){
		EntityManager em = EMF.getEM();
		EntityTransaction trans = em.getTransaction();
		try {
			trans.begin();
			em.merge(managerPayment);
		}catch(Exception e){
			System.err.println("异常问题");
			e.printStackTrace();
		}
		finally {
			trans.commit();
		}
	}
	
	/**
	 * 获取店主支付方式.
	 * 
	 * @param memberId
	 * @return
	 */
	public ManagerPayment getPayment(Long id){
		EntityManager em = EMF.getEM();
		ManagerPayment managerPayment = null;
		try{
			managerPayment = em.find(ManagerPayment.class, id);
		}catch(Exception e){
			e.printStackTrace();
		}
		return managerPayment;
	}
	
	/**
	 * 根据店主名称+支付id取店主支付方式.
	 * @param manager
	 * @param payId
	 * @return
	 */
	public ManagerPayment getByPayIdAndManager(String manager,Long payId){
		EntityManager em = EMF.getEM();
		Query query = em.createQuery("select  from ManagerPayment  mp where  mp.manager=:manager and mp.payId=:payId");
		query.setParameter("manager", manager);
		query.setParameter("payId", payId);
		List<ManagerPayment> list = query.getResultList();
		ManagerPayment mp = null;
		if(list.size() > 0){
			mp = list.get(0);
		}
		return mp;
	}
	
	/**
	 * 店主删除支付方式.
	 * 
	 * @param id
	 */
	public void delPayment(Long id){
		EntityManager em = EMF.getEM();
		EntityTransaction trans = em.getTransaction();
		try{
			trans.begin();
			ManagerPayment managerPayment = em.find(ManagerPayment.class, id);
			if(managerPayment != null){
				em.remove(managerPayment);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			trans.commit();
		}
	}
	
	/**
	 * 店主支付方式列表.
	 * 
	 * @param page
	 * @return
	 */
	public List<ManagerPayment> listPayment(String manager,Page page){
		EntityManager em = EMF.getEM();
		Query query = em.createQuery("select  from ManagerPayment  mp where    mp.manager=:manager");
		query.setParameter("manager", manager);
		page.setTotalCount(query.getResultList().size());
		query.setFirstResult(page.getFirst());
		query.setMaxResults(page.getPageSize());
		List<ManagerPayment> list = query.getResultList();
		return list;
	}
	
	/**
	 * 根据payId列表店主支付方式.
	 * 
	 * @param payId
	 * @return
	 */
	public List<ManagerPayment> listByPayId(Long payId){
		EntityManager em = EMF.getEM();
		Query query = em.createQuery("select  from ManagerPayment  mp where    mp.payId=:payId");
		query.setParameter("payId", payId);
		List<ManagerPayment> list = query.getResultList();
		return list;
	}
}
